package com.adcc.swingui;

import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.jidesoft.action.DefaultDockableBarDockableHolder;
import com.jidesoft.docking.DockableFrame;
import com.jidesoft.docking.DockableFrameFactory;

/**
 * 主界面
 * @author charlie
 *
 */
public class DefaultFrameUi extends DefaultDockableBarDockableHolder {
	private static final long serialVersionUID = 1L;
	private String imgPath;
	/**
	 * 构造器 ，默认图片资源路径res/images/
	 * @param title 标题
	 * @throws HeadlessException
	 */
    public DefaultFrameUi(String title) throws HeadlessException {
        super(title);
        imgPath = "res/images/";
    }
    /**
     * 构造器
     * @param title 标题
     * @param imgPath 图片路径
     * @throws HeadlessException
     */
    public DefaultFrameUi(String title, String imgPath) throws HeadlessException {
        super(title);
        this.imgPath = imgPath;
    }

    /**
     * Loads initial layout data from file.
     *
     * @param fileName
     * @throws SAXException
     * @throws ParserConfigurationException
     * @throws IOException
     */
    public void loadInitialLayout(String fileName) throws SAXException, ParserConfigurationException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(fileName));
        loadInitialLayout(document);
    }

    /**
     * Loads initial layout data from Document.
     *
     * @param document
     */
    public void loadInitialLayout(Document document) {

        ////////////////////////////////////////////////
        // Begin to creates and adds DockableFrames
        //

        getDockingManager().beginLoadLayoutData();

        // TODO: add dockable frames code here
        // TODO: after you added all dockable frames (make sure the key matches) defined in the initial layout,
        // TODO: you can remove the following call to setDockableFrameFactory.
        getDockingManager().setDockableFrameFactory(new DockableFrameFactory() {
            public DockableFrame create(String key) {
            	//根据key找对应的文件
            	ImageIcon icon = new ImageIcon(imgPath + "/" + key + ".png");
            	DockableFrame frame = null;
            	if(icon.getIconHeight() > 0) {
                    frame = new DockableFrame(key, icon);
            	} else {
            		frame = new DockableFrame(key);
            	}
                return frame;
            }
        });
        getDockingManager().loadInitialLayout(document);
        //
        // End of creates and add DockableFrames
        ////////////////////////////////////////////////
        
    }

}
